import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.factory.Factory.Payment;
import com.alipay.easysdk.kernel.Config;
import com.alipay.easysdk.kernel.util.ResponseChecker;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;

public class Main {
    public static void main(String[] args) throws Exception {
        // 1. 设置参数（全局只需设置一次）
        Factory.setOptions(getOptions());
        try {
            // 2. 发起API调用（以创建当面付收款二维码为例）              商品名称         编号，使用支付编号，业务比较简单，也可以直接使用订单编号   支付总金额   成功支付之后跳转的页面
            AlipayTradePagePayResponse response = Payment.Page().pay("Apple iPhone11 128G", "2234567890", "5799.00","");
            // 3. 处理响应或异常
            if (ResponseChecker.success(response)) {
                System.out.println(response.body);
                System.out.println("调用成功");
            } else {
                System.err.println("调用失败，原因：" + response.body);
            }
        } catch (Exception e) {
            System.err.println("调用遭遇异常，原因：" + e.getMessage());
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    private static Config getOptions() {
        Config config = new Config();
        config.protocol = "https";
        config.gatewayHost = "openapi.alipaydev.com/gateway.do";
        config.signType = "RSA2";
        config.appId = "2021000118603754";

        // 为避免私钥随源码泄露，推荐从文件中读取私钥字符串而不是写入源码中
        config.merchantPrivateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCSs3Zqfn8OwHWsNBR78FtBJHen9/yzdLEputANz+wKh6DzfAjJMpIypi2UeX4B29QNl5gcDc8gBgGckO7iEhtFY9pvF5amYeldSABW9N1iOXwDKmsBdLuvOpBcIsajq3bjmIbXulOKCsD1PKyXYd+h5SeRNw1c3SNjQr/flrCneIB0xGQ8C74QE6QG3Gl5tE0cwAojrviwuHY1ZCd1RKSW0o3sNPhI+pJOOtKkjcrbDOSwMaCMMzyXZvXLQBzokXzZD65EuhQvJjOdD6gCOafCQWuLREhfVGxidJGMplcFxpp/fweM945WPZ/gc7zLAMNdw2ZD+6GLrts+OxcMoE7ZAgMBAAECggEAI1xYvcCx9adUdeazzVAZXvFgWj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QEv7Pxzkjl9r7jFDpPQ0zuwjfLIjPNpGo4dsG3OocgZ/UTiqjwFrNE3/85Sgt+5jRgOW3cUrDUDVSMHZ3C0EZw+H4UFqp0rtt0Du8ZqPc/10hCgoOVW/uyTr3Th0nc";

        //注：如果采用非证书模式，则无需赋值上面的三个证书路径，改为赋值如下的支付宝公钥字符串即可
        config.alipayPublicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIgHnOn7LLILlKETd6BFRJ0GqgS2Y3mn1wMQmyh9zEyWlz5p1zrahRahbXAfCfSqshSNfqOmAQzSHRVjCqjsAw1jyqrXaPdKBmr90DIpIxmIyKXv4GGAkPyJ/6FTFY99uhpiq0qadD/uSzQsefWo0aTvP/65zi3eof7TcZ32oWpwIDAQAB";

        //可设置异步通知接收服务地址（可选）
        config.notifyUrl = "<-- 请填写您的支付类接口异步通知接收服务地址，例如：https://www.test.com/callback -->";

        return config;
    }
}